﻿using TheVerge.Base.Platform;

namespace TheVerge.Base.Models
{
    public class VMLogin : AVM
    {
        public VMLogin()
        {
            IsLoggedIn = API.IsLoggedIn;
        }

        public bool IsLoggedIn
        {
            get { return GetValue(() => IsLoggedIn); }
            private set { SetValue(value, () => IsLoggedIn); }
        }

        public bool IsAuthenticating
        {
            get { return GetValue(() => IsAuthenticating); }
            private set { SetValue(value, () => IsAuthenticating); }
        }

        public void Login(string userName, string password)
        {
            IsAuthenticating = true;
            API.Login(userName, password, () =>
            {
                IsAuthenticating = false;
                IsLoggedIn = true;
            }, () => 
            {
                IsAuthenticating = false;
                Factory.UI.ShowMessage("There was a problem logging in. Please ensure your credentials are correct and try again.");
            });
        }

        public void Logout()
        {
            API.Logout();
            IsLoggedIn = false;
        }
    }
}
